package com.qdh.demotest;

import com.qdh.WordCountJob;



import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class SubJob {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        //参数非空判断
        if (args.length != 2) {
            System.out.println("参数个数不对");
            System.exit(0);
        }
        // 1. 初始化配置
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop10:8020");
        //2. 创建job
        Job job = Job.getInstance(conf);
        job.setJarByClass(WordCountJob.class);

        //3. 设置输入格式化工具和输出格式化
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        //4. 设置输入路径和输出路径
/*        TextInputFormat.addInputPath(job,new Path("/score.txt"));
        TextOutputFormat.setOutputPath(job,new Path("/outScore"));*/
        TextInputFormat.addInputPath(job,new Path(args[0]));
        TextOutputFormat.setOutputPath(job,new Path(args[1]));

        //5. 设置mapper和reducer
        job.setMapperClass(SubMapper.class);
        job.setReducerClass(SubReducer.class);

        // 6. 设置mapper的kv类型和reducer的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Subjectnew.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Subjectnew.class);

        //reduceTask数量要和分区数量一样。

        // 7. 启动job
        boolean b = job.waitForCompletion(true);
        System.out.println(b);

    }
    static class SubMapper extends Mapper<LongWritable, Text, Text, Subjectnew> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Subjectnew>.Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().split(" ");
/*            System.out.println(splits[1]);
            System.out.println(splits[2]);
            System.out.println(splits[3]);
            System.out.println(splits[4]);*/

            int chinese = Integer.parseInt(splits[2]);
            int english = Integer.parseInt(splits[3]);
            int math = Integer.parseInt(splits[4]);
            int total = chinese + english + math;

            context.write(new Text(splits[1]),
                    new Subjectnew(chinese,english,math,total));



        }
    }
    static class SubReducer extends Reducer<Text, Subjectnew, Text, Subjectnew> {
        @Override
        protected void reduce(Text key, Iterable<Subjectnew> values, Reducer<Text, Subjectnew, Text, Subjectnew>.Context context) throws IOException, InterruptedException {

            for (Subjectnew value : values) {
                context.write(key,value);
            }
        }
    }

}

